using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Pages;

namespace SharpMedia.Controls
{
    public class HUDVideoOptions : HUDContainerSimple
    {
        HUDVideo2 video;
        
        public HUDVideo2 Video
        {
            get { return video; }
            set { video = value; }
        }

        HUDContainerSimple ctxContainer, ctxCentering;
        HUDCenterList ctxList;
        HUDLabel ctxSelectionLabel;

        HUDContainerSimple contextControls = new HUDContainerSimple();

        /*
        HUDLabel sldLblMinValue, sldLblValue, sldLblMaxValue;
        HUDContainerSimple sldContainer;
        HUDVertBar sldBar;

        HUDContainerSimple listContainer, listBox;
        HUDMoveList listList;
        HUDImage listSelection;
        */
        Lizk.SimpleHUD.TripTrigger trigger = new Lizk.SimpleHUD.TripTrigger(new TimeSpan(0, 0, 0, 0, 200));

        

        public int SelectedIndex
        {
            get { return ctxList.SelectedIndex; }
            set { ctxList.SelectedIndex = value; }
        }


        public ContextBarItem SelectedItem
        {
            get { return ctxList.SelectedControl.GetTag<ContextBarItem>(); }
        }

        public void Up()
        {
            if (ctxList.SelectedControl != null)
                ((ContextBarItem)ctxList.SelectedControl.Tag).Up();
        }
        public void Down()
        {
            if (ctxList.SelectedControl != null)
                ((ContextBarItem)ctxList.SelectedControl.Tag).Down();
        }
        public void OkPressed()
        {
            if (ctxList.SelectedControl != null)
                ((ContextBarItem)ctxList.SelectedControl.Tag).Ok();
        }

        public HUDVideoOptions(HUDVideo2 video)
        {
            this.video = video;
            InitializeComponents();
        }

        public bool Contains(ContextBarItem item)
        {
            foreach (HUDControl hc in ctxList)
            {
                if (hc.Tag == item)
                    return true;
            }
            return false;
        }

        public void Remove(ContextBarItem item)
        {
            for (int i = 0; i < ctxList.Count; i++)
            {
                if (ctxList[i].Tag == item)
                {
                    ctxList.RemoveAt(i);
                    contextControls.RemoveAt(i);
                    return;
                }
            }
        }


        private void InitializeComponents()
        {

            ctxContainer = new HUDContainerSimple();
            ctxContainer.Bounds = new RectangleS(0, 0, Bounds.Width, 110);
            ctxContainer.Anchor = Anchor.All ^ Anchor.Top;
            ctxContainer.Alpha = 255;

            ctxCentering = new HUDCentering(this, ctxContainer);
            ctxCentering.Bounds = new RectangleS(0, 0, Bounds.Width, 110);
            ctxCentering.Anchor = Anchor.All ^ Anchor.Bottom;
            Add(ctxCentering);

            contextControls.Bounds = new RectangleS(0, 110, Bounds.Width, Bounds.Height - 110);
            contextControls.Anchor = Anchor.All ^ Anchor.Bottom;
            Add(contextControls);


            HUDImage ctxBarBackground = new HUDImage("white.png");
            ctxBarBackground.Color = Lizk.SimpleHUD.ColorS.Black;
            ctxBarBackground.Alpha = 127;
            ctxBarBackground.Anchor = Anchor.All;
            ctxBarBackground.ConstrainProportions = false;
            ctxBarBackground.Size = ctxContainer.Size;
            ctxContainer.Add(ctxBarBackground);

            ctxList = new HUDCenterList(new SizeS(70, 70));
            ctxList.Bounds = new RectangleS(0, 35, ctxContainer.Bounds.Width, 70);
            ctxList.Anchor = Anchor.All ^ Anchor.Bottom;
            ctxContainer.Add(ctxList);

            ctxList.SelectedIndexChanged += new EventHandler<SelectionEventArgs>(ctxList_SelectedIndexChanged);
            ctxList.SelectedIndex = 0;
            ctxList.MoveItems(false);

            ctxSelectionLabel = new HUDLabel("", "tahoma30b");
            ctxSelectionLabel.Bounds = new RectangleS(0, 0, ctxContainer.Bounds.Width, 50);
            ctxSelectionLabel.Alignment = Alignment.Top | Alignment.Center;
            ctxSelectionLabel.Anchor = Anchor.All ^ Anchor.Top;
            ctxContainer.Add(ctxSelectionLabel);

            trigger.TimeOut += new EventHandler(trigger_TimeOut);

        }

        void trigger_TimeOut(object sender, EventArgs e)
        {
            ctxSelectionLabel.Text = (string)ctxList.GetSelectedControl<HUDContainerSimple>().Get<HUDImage>(1).Tag;
            ctxList.GetSelectedControl<HUDContainerSimple>().Get<HUDImage>(0).Fade(255, 6f);

            SelectedItem.Control.Fade(255, 6f);

            contextControls.Size = new SizeS(contextControls.Size.Width, SelectedItem.Control.Size.Height);

            ctxSelectionLabel.Fade(255, 6f);
        }

        void ctxList_SelectedIndexChanged(object sender, SelectionEventArgs e)
        {
            if (e.OldIndex > -1 && e.OldIndex < ctxList.Count)
            {
                ((ContextBarItem)ctxList[e.OldIndex].Tag).Deselect();
                ctxList.Get<HUDContainerSimple>(e.OldIndex).Get<HUDImage>(0).Fade(0, 6f);
                contextControls.Get<HUDCentering>(e.OldIndex)[0].Fade(0, 6f);
            }


            trigger.Trigger();
            
            ContextBarItem item = (ContextBarItem)ctxList.SelectedControl.Tag;
            item.Selected();


            ctxSelectionLabel.Fade(0, 6f);
        }
        public void AddItem(ContextBarItem item)
        {
            HUDContainerSimple s = new HUDContainerSimple();
            s.Size = new SizeS(70,70);
            s.Tag = item;
            ctxList.Add(s);

            HUDImage bg = new HUDImage("glow.png");
            bg.Size = new SizeS(70, 70);
            bg.Alpha = 0;
            s.Add(bg);

            HUDImage img = new HUDImage(item.Image);
            img.Size  = new SizeS(64, 64);
            img.Location = new PointS(3, 3);
            img.Tag = item.Title;
            s.Add(img);

            contextControls.Add(new HUDCentering(contextControls, item.Control) { Size = contextControls.Size, Anchor = Anchor.All });


            if(ctxList.Count == 1)
                ctxList.SelectedIndex = 0;
            ctxList.MoveItems(false);


        }


        public override void Update(TimeSpan ts)
        {
            base.Update(ts);
            trigger.Update();
            foreach (HUDControl h in ctxList)
            {
                ((ContextBarItem)h.Tag).Update(ts);
            }
        }

    }


    public abstract class ContextBarItem
    {
        private string title;

        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        private string image;

        public string Image
        {
            get { return image; }
            set { image = value; }
        }
        private HUDVideoOptions options;

        public HUDVideoOptions Options
        {
            get { return options; }
            set { options = value; }
        }

        public HUDControl Control { get; set; }

        public virtual void Update(TimeSpan ts) { }

        public ContextBarItem(HUDVideoOptions options, string title, string image)
        {
            this.title = title;
            this.image = image;
            this.options = options;
        }

        public abstract void Deselect();
        public abstract void Selected();
        public abstract void Ok();
        public abstract void Up();
        public abstract void Down();
        public abstract void Reapply();
    }

    public abstract class SliderContextBarItem : ContextBarItem
    {
        protected float value;

        public float Value
        {
            get { return value; }
            set { this.value = value; }
        }
        protected HUDLabel sldLblValue, sldLblMaxValue, sldLblMinValue;
        protected HUDVertBar sldBar;
        public SliderContextBarItem(HUDVideoOptions options, string title, string image) : base(options, title, image) 
        {
            HUDContainerSimple sliderContainer = new HUDContainerSimple();
            sliderContainer.Size = new SizeS(105, 200);
            sliderContainer.Alpha = 0;
            HUDImage bg = new HUDImage("white.png");
            bg.Size = sliderContainer.Size;
            bg.Color = ColorS.Black;
            bg.Alpha = 127;
            bg.ConstrainProportions = false;
            sliderContainer.Add(bg);

            sldLblValue = new HUDLabel("-100", "verdana22b2");
            sldLblValue.Size = new SizeS(100, 200);
            sldLblValue.Location = new PointS(40, 0);
            sldLblValue.Alignment = Alignment.Left | Alignment.VCenter;
            sliderContainer.Add(sldLblValue);

            sldLblMaxValue = new HUDLabel("100", "tahoma14b");
            sldLblMaxValue.Size = new SizeS(100, 20);
            sldLblMaxValue.Location = new PointS(40, 0);
            sldLblMaxValue.Alignment = Alignment.Left | Alignment.VCenter;
            sliderContainer.Add(sldLblMaxValue);

            sldLblMinValue = new HUDLabel("-100", "tahoma14b");
            sldLblMinValue.Size = new SizeS(100, 20);
            sldLblMinValue.Location = new PointS(40, 180);
            sldLblMinValue.Alignment = Alignment.Left | Alignment.VCenter;
            sliderContainer.Add(sldLblMinValue);

            sldBar = new HUDVertBar();
            sldBar.Size = new SizeS(27, 180);
            sldBar.Location = new PointS(10, 10);
            sldBar.Value = 50;
            sliderContainer.Add(sldBar);

            Control = sliderContainer;
        }

        public override void Deselect()
        {
            
        }
        public override void Selected()
        {
            
        }


    }

    public abstract class ListContextBarItem : ContextBarItem
    {
        HUDImage listSelection;
        protected HUDMoveList list;

        public ListContextBarItem(HUDVideoOptions options, string title, string image) : base(options, title, image) 
        {
            HUDContainerSimple listBox = new HUDContainerSimple();
            listBox.Size = new SizeS(210, 100);
            listBox.Alpha = 0;

            HUDImage bg = new HUDImage("white.png");
            bg.Size = listBox.Size;
            bg.Color = ColorS.Black;
            bg.Alpha = 127;
            bg.ConstrainProportions = false;
            bg.Anchor = Anchor.All;
            listBox.Add(bg);

            listSelection = new HUDImage("white.png");
            listSelection.Size = new SizeS(210, 30);
            listSelection.ConstrainProportions = false;
            listSelection.Color = ColorS.Green;
            listSelection.Alpha = 127;
            listBox.Add(listSelection);



            list = new HUDMoveList(new SizeS(210, 30), false);
            list.Bounds = new RectangleS(0, 0, 280, 100);
            list.Anchor = Anchor.All;
            listBox.Add(list);

            Control = listBox;
        }

        public override void Deselect()
        {
        }
        public override void Selected()
        {
        }
        public override void Up()
        {
            list.SelectedIndex--;
            UpdateList(true);
        }
        public override void Down()
        {
            list.SelectedIndex++;
            UpdateList(true);
        }


        protected void UpdateList(bool animate)
        {
            list.MoveItems(animate);
            listSelection.Destination = list.Selection.Location + list.Location;
            listSelection.DestinationSize = list.Selection.Size;

            if (!animate)
            {
                listSelection.Location = list.Selection.Location + list.Location;
                listSelection.Size = list.Selection.Size;
            }

        }
    }

    public class AudioTrackSelector : ListContextBarItem
    {
        private int selectedAudioTrack = 0;
        public AudioTrackSelector(HUDVideoOptions options, string title, string image)
            : base(options, title, image)
        {
            languageTrigger.TimeOut += new EventHandler(languageTrigger_TimeOut);
        }

        void languageTrigger_TimeOut(object sender, EventArgs e)
        {
            selectedAudioTrack = Options.Video.ActiveAudiotrack = list.SelectedIndex;
            //Options.video.ActiveAudiotrack = ((Lizk.SimpleHUD.HUDVideo2.TrackIdentifier)Options.listList.SelectedControl.Tag).id;
        }


        TimedTrigger<string> languageTrigger = new TimedTrigger<string>(null, new TimeSpan(0, 0, 1));
        public override void Deselect()
        {
            base.Deselect();
        }

        public void UpdateTracks()
        {
           
            list.Clear();
            Control.Size = new SizeS(Control.Size.Width, list.ItemSize.Height * Options.Video.Audiotracks.Count);
            //Options.listContainer.Location = new PointS(0, 110);
            
            int i = 0;
            foreach (Lizk.SimpleHUD.HUDVideo2.TrackIdentifier ti in Options.Video.Audiotracks)
            {

                HUDContainerSimple hat = new HUDContainerSimple();
                hat.Size = list.ItemSize;
                hat.Tag = ti;
                list.Add(hat);
                if (i == Options.Video.ActiveAudiotrack)
                    list.SelectedIndex = i;
                HUDLabel lbl = new HUDLabel(ti.name, "tahoma23b");
                lbl.Size = list.ItemSize;
                hat.Add(lbl);
                i++;
            }

            UpdateList(false);
        }

        public override void Selected()
        {
            base.Selected();


            UpdateTracks();

        }

        public override void Ok()
        {
        }

        public override void Up()
        {
            base.Up();
            languageTrigger.Trigger();

        }

        public override void Down()
        {
            base.Down();
            languageTrigger.Trigger();
        }

        public override void Update(TimeSpan ts)
        {
            base.Update(ts);
            languageTrigger.Update();
        }

        public override void Reapply()
        {
            Options.Video.ActiveAudiotrack = selectedAudioTrack;
        }
    }
    public class SubtitleTrackSelector : ListContextBarItem
    {
        private int selectedSubtitle = 0;
        public SubtitleTrackSelector(HUDVideoOptions options, string title, string image)
            : base(options, title, image)
        {
            languageTrigger.TimeOut += new EventHandler(languageTrigger_TimeOut);
        }

        void languageTrigger_TimeOut(object sender, EventArgs e)
        {
            selectedSubtitle = Options.Video.ActiveSubtitle = list.SelectedIndex;
        }


        TimedTrigger<string> languageTrigger = new TimedTrigger<string>(null, new TimeSpan(0, 0, 1));
        public override void Deselect()
        {
            base.Deselect();
        }

        public void UpdateTracks()
        {
/*            if (Options.ctxList.SelectedControl.Tag != this)
                return;
            */
            list.Clear();
            Control.Size = new SizeS(Control.Size.Width, list.ItemSize.Height * Options.Video.Subtitles.Count);
            //Options.listContainer.Location = new PointS(0, 110);
            int i = 0;
            foreach (Lizk.SimpleHUD.HUDVideo2.TrackIdentifier ti in Options.Video.Subtitles)
            {
                HUDContainerSimple hat = new HUDContainerSimple();
                hat.Size = list.ItemSize;
                hat.Tag = ti;
                list.Add(hat);
                if (i == Options.Video.ActiveSubtitle)
                    list.SelectedIndex = i;
                HUDLabel lbl = new HUDLabel(ti.name, "tahoma23b");
                lbl.Size = list.ItemSize;
                hat.Add(lbl);
                i++;
            }

            UpdateList(false);
        }

        public override void Selected()
        {
            base.Selected();


            UpdateTracks();

        }

        public override void Ok()
        {
        }
        public override void Up()
        {
            base.Up();
            languageTrigger.Trigger();

        }

        public override void Down()
        {
            base.Down();
            languageTrigger.Trigger();
        }
        public override void Update(TimeSpan ts)
        {
            base.Update(ts);
            languageTrigger.Update();
        }
        public override void Reapply()
        {
            Options.Video.ActiveSubtitle = selectedSubtitle;
        }
    }

    public class ZoomSelector : ListContextBarItem
    {
        private enum Zoom
        {
            Normal,
            Stretch,
            FullscreenToWidescreen
        }
        private Zoom currentZoom = Zoom.Normal;

        public ZoomSelector(HUDVideoOptions options, string title, string image)
            : base(options, title, image)
        {

            list.Clear();
            Control.Size = new SizeS(Control.Size.Width, list.ItemSize.Height * 3);
            //Options.listContainer.Location = new PointS(0, 110);

            HUDContainerSimple hat = new HUDContainerSimple();
            hat.Size = list.ItemSize;
            hat.Tag = Zoom.Normal;
            list.Add(hat);

            HUDLabel lbl = new HUDLabel("No Zoom", "tahoma23b");
            lbl.Size = list.ItemSize;
            hat.Add(lbl);

            hat = new HUDContainerSimple();
            hat.Size = list.ItemSize;
            hat.Tag = Zoom.Stretch;
            list.Add(hat);

            lbl = new HUDLabel("Stretch", "tahoma23b");
            lbl.Size = list.ItemSize;
            hat.Add(lbl);

            hat = new HUDContainerSimple();
            hat.Size = list.ItemSize;
            hat.Tag = Zoom.FullscreenToWidescreen;
            list.Add(hat);

            lbl = new HUDLabel("Zoom 16:9", "tahoma23b");
            lbl.Size = list.ItemSize;
            hat.Add(lbl);
        }

        public override void Selected()
        {
            base.Selected();

            /*if (Options.ctxList.SelectedControl.Tag != this)
                return;
            */

            UpdateList(false);
        }

        public override void Up()
        {
            base.Up();
            currentZoom = (Zoom)list.SelectedControl.Tag;
            SetZoom(currentZoom);
        }

        public override void Down()
        {
            base.Down();
            currentZoom = (Zoom)list.SelectedControl.Tag;
            SetZoom(currentZoom);
        }

        private void SetZoom(Zoom z)
        {
            switch (z)
            {
                case Zoom.Stretch:
                    Options.Video.ConstrainProportions = false;
                    Options.Video.Bounds = Options.Bounds;
                    break;
                case Zoom.FullscreenToWidescreen:
                    Options.Video.ConstrainProportions = false;
                    RectangleS r = HUDImage.ResizeConstrained(true, new SizeS(4, 3), Options.Bounds.Size);
                    SizeS dsage = new SizeS(Options.Bounds.Width, Options.Bounds.Height * Options.Bounds.Width / r.Width);
                    PointS pso = new PointS(0, -(dsage.Height - Options.Bounds.Height) / 2);
                    Options.Video.Bounds = new RectangleS(pso, dsage);
                    break;
                case Zoom.Normal:
                    Options.Video.Bounds = Options.Bounds;
                    Options.Video.ConstrainProportions = true;
                    break;
            }
        }


        public override void Ok()
        {

        }
        public override void Reapply()
        {
            SetZoom(currentZoom);
        }
    }

    public class BrightnessSelector : SliderContextBarItem
    {
        public BrightnessSelector(HUDVideoOptions options, string title, string image) : base(options, title, image) { }

        public override void Selected()
        {
            base.Selected();
            UpdateLabels();

        }

        private void UpdateLabels()
        {
            sldLblValue.Text = (sldBar.Value = Options.Video.Brightness).ToString();
            sldLblMaxValue.Text = (sldBar.MaxValue = Options.Video.MaxBrightness).ToString();
            sldLblMinValue.Text = (sldBar.MinValue = Options.Video.MinBrightness).ToString();
        }

        public override void Ok()
        {
        }

        public override void Up()
        {
            value = Options.Video.Brightness += Options.Video.StepBrightness;
            UpdateLabels();
        }

        public override void Down()
        {
            value = Options.Video.Brightness -= Options.Video.StepBrightness;
            UpdateLabels();
        }
        public override void Reapply()
        {
            Options.Video.Brightness = value;
        }
    }

    public class ContrastSelector : SliderContextBarItem
    {

        public ContrastSelector(HUDVideoOptions options, string title, string image) : base(options, title, image) { }

        public override void Selected()
        {
            base.Selected();

            UpdateLabels();

        }

        private void UpdateLabels()
        {
            sldLblValue.Text = (value = sldBar.Value = Options.Video.Contrast).ToString();
            sldLblMaxValue.Text = (sldBar.MaxValue = Options.Video.MaxContrast).ToString();
            sldLblMinValue.Text = (sldBar.MinValue = Options.Video.MinContrast).ToString();
        }

        public override void Ok()
        {
        }

        public override void Up()
        {
            value = Options.Video.Contrast += Options.Video.StepContrast * 2f;
            UpdateLabels();
        }

        public override void Down()
        {
            value = Options.Video.Contrast -= Options.Video.StepContrast * 2f;
            UpdateLabels();
        }
        public override void Reapply()
        {
            Options.Video.Contrast = value;
        }
    }
    public class SaturationSelector : SliderContextBarItem
    {
        public SaturationSelector(HUDVideoOptions options, string title, string image) : base(options, title, image) { }

        public override void Selected()
        {
            base.Selected();

            UpdateLabels();

        }

        private void UpdateLabels()
        {
            sldLblValue.Text = (value = sldBar.Value = Options.Video.Saturation).ToString();
            sldLblMaxValue.Text = (sldBar.MaxValue = Options.Video.MaxSaturation).ToString();
            sldLblMinValue.Text = (sldBar.MinValue = Options.Video.MinSaturation).ToString();
        }

        public override void Ok()
        {
        }

        public override void Up()
        {
            value = Options.Video.Saturation += Options.Video.StepSaturation * 2f;
            UpdateLabels();
        }

        public override void Down()
        {
            value = Options.Video.Saturation -= Options.Video.StepSaturation * 2f;
            UpdateLabels();
        }
        public override void Reapply()
        {
            Options.Video.Saturation = value;
        }
    }
    public class HueSelector : SliderContextBarItem
    {
        public HueSelector(HUDVideoOptions options, string title, string image) : base(options, title, image) { }

        public override void Selected()
        {
            base.Selected();

            UpdateLabels();

        }

        private void UpdateLabels()
        {
            sldLblValue.Text = (value = sldBar.Value = Options.Video.Hue).ToString();
            sldLblMaxValue.Text = (sldBar.MaxValue = Options.Video.MaxHue).ToString();
            sldLblMinValue.Text = (sldBar.MinValue = Options.Video.MinHue).ToString();
        }

        public override void Ok()
        {
        }

        public override void Up()
        {
            value = Options.Video.Hue += Options.Video.StepHue;
            UpdateLabels();
        }

        public override void Down()
        {
            value = Options.Video.Hue -= Options.Video.StepHue;
            UpdateLabels();
        }
        public override void Reapply()
        {
            Options.Video.Hue = value;
        }
    }

    public class YesNoSelector : ListContextBarItem
    {
        public bool Enabled { get; set; }

        public YesNoSelector(HUDVideoOptions options, string title, string image)
            : base(options, title, image)
        {
            list.Clear();
            Control.Size = new SizeS(Control.Size.Width, list.ItemSize.Height * 2);
            //Options.listContainer.Location = new PointS(0, 110);

            HUDContainerSimple hat = new HUDContainerSimple();
            hat.Size = list.ItemSize;
            hat.Tag = true;
            list.Add(hat);

            HUDLabel lbl = new HUDLabel("Yes", "tahoma23b");
            lbl.Size = list.ItemSize;
            hat.Add(lbl);

            hat = new HUDContainerSimple();
            hat.Size = list.ItemSize;
            hat.Tag = false;
            list.Add(hat);

            lbl = new HUDLabel("No", "tahoma23b");
            lbl.Size = list.ItemSize;
            hat.Add(lbl);
        }

        public override void Selected()
        {
            base.Selected();
            list.SelectedIndex = Enabled ? 0 : 1;
            UpdateList(false);
        }

        public override void Up()
        {
            base.Up();
            Enabled = (bool)list.SelectedControl.Tag;
        }

        public override void Down()
        {
            base.Down();
    
            Enabled = (bool)list.SelectedControl.Tag;
        }



        public override void Ok()
        {

        }
        public override void Reapply()
        {
        }
    }



}
